<!-- CONTENT AREA -->
<div id='content'>
<center>
<script> 
storage      = new Array(0, 99994554.6111, 99986188.16, 99988098.7194);
factor       = new Array(0, 3, 1.8, 0.84);
offer_id     = 1;
offer_amount = 5445;
offer_costs  = 0;
 
function number_format(number, decimals, dec_point, thousands_sep) {
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  if (decimals != null)  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
    fractional += "0";
  }
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep : null;
  if (thousands_sep != null && thousands_sep != ""){
    for (i = integer.length - 3; i > 0; i -= 3)integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  return sign + integer + fractional + exponent;
}
 
 
 
function setStorage(id, value) {
    document.getElementById(id + '_storage').innerHTML = number_format(value, 0, '', '.');
}
 
function setValue(id, value) {
    if (id != offer_id) document.getElementsByName(id + '_value')[0].value = number_format(value, 0, '', '.');
    else                document.getElementById(id + '_value').innerHTML   = number_format(value, 0, '', '.');
}
 
 
function getValue(id) {
    if (id != offer_id) result = document.getElementsByName(id + '_value')[0].value.replace(/^0+/,"");
    else                result = document.getElementById(id + '_value').innerHTML.replace(/^0+/,"");
    result = parseInt(result.split('.').join(''));
    if (isNaN(result)) return 0;
    else               return result;
}
 
function calcCosts(id, amount) {
    return Math.floor(amount * factor[offer_id] / factor[id]);
}
 
function calcInputFromCosts(id, amount) {
    return Math.max(Math.round(amount / factor[offer_id] * factor[id]),0);
}
 
function displayOfferCosts() {
    setValue(offer_id, offer_costs);
}
 
function getFreeOfferCosts() {
    value = Math.round(offer_amount - (offer_costs));
    return value;
}
 
function addOfferCosts(costs) {
    offer_costs = offer_costs + costs;
}
 
function checkValue(id) {
    if (getValue(id) < 0) {
        setValue(id, getValue(id) * -1);
    } 
 
    if (getValue(id) > storage[id]) {
        setValue(id, storage[id]);
    }   
    free_id     = 6 - id - offer_id; 
    offer_costs = calcCosts(free_id, getValue(free_id));
    costs = calcCosts(id, getValue(id));
    if (costs > getFreeOfferCosts()) {
        setValue(id, calcInputFromCosts(id, getFreeOfferCosts()));
        costs = calcCosts(id, getValue(id));
    }
    addOfferCosts(costs);
    displayOfferCosts();
    setStorage(id, storage[id] - getValue(id)); 
}
 
function setMaxValue(id) {
    setValue(id, 99999999999999);
    checkValue(id);
}
 
</script>
 
<form action="index.php?page=trader&session=35ce0f24d9e3" name="TraderForm" method="POST">
    <TABLE class="c" width='520px'>
        <tr>
            <td colspan=3 class="c" align='center'>Es ist ein Handler anwesend, der Metall kauft.</td>
            </tr>
        <tr>
            <th class="c" align='center'><br>
                Einen Handler holen, der                <select name="offer_id" style="color: lime;">
                  <option value="1" selected>Metall</option>
                  <option value="2" >Kristall</option>
                  <option value="3" >Deuterium</option>
                </select>       
                kauft               <br>
                <div id='darkmatter2'>(Kosten: 3500 Dunkle Materie)</div><br><br>
                
                <input type='submit' name='call_trader' value='Neuen Handler holen'>
            </th>
        </tr>
    </TABLE>    
    <br>
</form> 
<form action="index.php?page=trader&session=35ce0f24d9e3" name="TraderForm" method="POST">
    <TABLE width='520px'>
        <TR>
            <TD colspan=4 class="c" align='center'>Jetzt Rohstoffe tauschen</TD>
        </TR>
        
        <TR>
            <th></th>
            <th></th>
            <th>Freier Lagerraum</th>
            <th>Tauschkurse</th>
        </TR>
        
        
        <TR>
            <th class="c" align="center" width=25% >Metall</th>
                          <th class="c" align='center' width=25% ><span id="1_value">0</span></th>
              <th class="c" align='center' width=25% >---</th>
                        
            <th class="c" align='center' width=25% >
                          <font size=3><b>3</b></font>
                        </th>           
        </TR>
        
        <TR>
            <th class="c" align="center" width=25% >Kristall</th>
                          <th class="c" align='center' width=25% ><input type="text" size="9" name="2_value" value="0" style="text-align:right;" onkeyup='checkValue(2);'> <a href="#" onClick="setMaxValue(2);">max</a></th>
              <th class="c" align='center' width=25% ><span id="2_storage">99.986.188</span></th>
                        
            <th class="c" align='center' width=25% >
                          <a href=# onmouseover="return overlib('<font color=white>Ein Metall ergibt 0.6 Kristall</font>');" onmouseout="return nd();">
                          <font size=3><b>1.8</b></font>
                          </a>
                        </th>           
        </TR>
        
        <TR>
            <th class="c" align="center" width=25% >Deuterium</th>
                          <th class="c" align='center' width=25% ><input type="text" size="9" name="3_value" value="0" style="text-align:right;" onkeyup='checkValue(3);'> <a href="#" onClick="setMaxValue(3);">max</a></th>
              <th class="c" align='center' width=25% ><span id="3_storage">99.988.099</span></th>
                        
            <th class="c" align='center' width=25% >
                          <a href=# onmouseover="return overlib('<font color=white>Ein Metall ergibt 0.28 Deuterium</font>');" onmouseout="return nd();">
                          <font size=3><b>0.84</b></font>
                          </a>
                        </th>           
        </TR>
        
        <tr>
        <th class="c" align="center" colspan=4 ><br>Ein Handler liefert maximal so viele Rohstoffe, wie freier Lagerraum zur Verfugung steht.       <br><br><input type=submit name='trade' value='Rohstoffe tauschen!'>
        </th>
        </tr>
    </TABLE>
</form> 
    <br><br><br><br>
</center>
</div>
<!-- END CONTENT AREA -->
